package com.zhc.dejingapi.utils.qingxi;

import com.google.gson.Gson;
import com.zhc.dejingapi.pojo.qingflow.QingflowApiInfo;
import com.zhc.dejingapi.pojo.qingxi.QingxiPassworkResultMainModel;
import com.zhc.dejingapi.pojo.qingxi.QingxiPubKeyBackResultMainModel;
import com.zhc.dejingapi.pojo.qingxi.QingxiUserTokenResultMainModel;
import com.zhc.dejingapi.utils.HttpClientUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
public class QingXiTokenUtils {

    public static String _QingxiUserToken = "";
    public static Date _QindxiExpireDate;

    public static Integer _QingxiWSId;
    //每天要登录一次



    public static String GetPubKey(QingflowApiInfo qingflowApiInfo) throws Exception {
        String strPubKey = "";
        String strUrl = qingflowApiInfo.getServerUrl();
        if (!strUrl.endsWith("/")) {
            strUrl = strUrl + "/";
        }
        strUrl += "api/user/pubkey";
        Gson gson = new Gson();
        Map<String, String> mapHead = new HashMap<>();

        String strBack = HttpClientUtil.doGet(strUrl, mapHead);
        if (strBack == null || strBack.isEmpty()) {
            throw new Exception("获取公钥失败");
        } else {
            QingxiPubKeyBackResultMainModel resultMainModel = gson.fromJson(strBack, QingxiPubKeyBackResultMainModel.class);
            if (resultMainModel == null) {
                throw new Exception("获取公钥失败,返回内容为空！");
            } else {
                if(resultMainModel.getCode() == 0){
                    if (resultMainModel.getData()==null||resultMainModel.getData().getPubkey()==null||resultMainModel.getData().getPubkey().isEmpty()){
                        throw new Exception("获取公钥失败,返回内容为空！");
                    }
                    else{
                        strPubKey = resultMainModel.getData().getPubkey();
                    }
                }
                else{
                    throw new Exception("获取公钥失败,"+resultMainModel.getMessage()+"！");
                }

            }
        }

        return strPubKey;
    }

    public static QingxiPassworkResultMainModel GetUserPWD(QingflowApiInfo qingflowApiInfo) throws Exception{
        String strTicket = "";

        String strUrl = qingflowApiInfo.getServerUrl();
        if (!strUrl.endsWith("/")) {
            strUrl = strUrl + "/";
        }
        strUrl += "api/user/pwd";
        Gson gson = new Gson();
        Map<String, String> mapHead = new HashMap<>();
        JSONObject obj = new JSONObject();

        String strUserPassWord = qingflowApiInfo.getUserPassWord();
        //加密成MD5
        String strRSAPubKey=GetPubKey(qingflowApiInfo);
        strUserPassWord=QingXiRSAUtils.encryptByPublicKey(strUserPassWord,strRSAPubKey);

        obj.put("password", strUserPassWord);
        obj.put("mobile", "");
        obj.put("email", qingflowApiInfo.getUserEmail());
        obj.put("areaCode", "86");
        obj.put("loginType", "email");
        String strBody=obj.toString();

        String strBack = HttpClientUtil.doPost(strUrl, strBody, mapHead);

        if (strBack == null || strBack.isEmpty()) {
            throw new Exception("获取公钥失败");
        } else {
            QingxiPassworkResultMainModel resultMainModel = gson.fromJson(strBack, QingxiPassworkResultMainModel.class);
            if (resultMainModel == null) {
                throw new Exception("获取公钥失败,返回内容为空！");
            } else {
                if(resultMainModel.getCode() == 0){
                    if (resultMainModel.getData()==null||resultMainModel.getData().getTicket()==null||resultMainModel.getData().getTicket().isEmpty()){
                        throw new Exception("获取公钥失败,返回内容为空！");
                    }
                    else{
                        //strTicket = resultMainModel.getData().getTicket();
                        return resultMainModel;
                    }
                }
                else{
                    throw new Exception("获取公钥失败,"+resultMainModel.getMessage()+"！");
                }

            }
        }
        //return null;
    }


    public static String GetUserToken(QingflowApiInfo qingflowApiInfo) throws Exception{
        boolean bNeedGet = false;
        if (_QingxiUserToken == null || _QingxiUserToken.isEmpty()) {
            bNeedGet = true;
        } else if (_QindxiExpireDate.compareTo(new Date()) <= 0) {
            bNeedGet = true;
        }


        if (bNeedGet) {
            // 获取当前时间
            Date currentTime = new Date();
            //重新获取token

            QingxiPassworkResultMainModel passworkResultMainModel=GetUserPWD(qingflowApiInfo);
            if(passworkResultMainModel==null){
                throw new Exception("获取用户PWD失败！");
            }

            _QingxiWSId=passworkResultMainModel.getData().getWsId();
            String strUrl = qingflowApiInfo.getServerUrl();
            if (!strUrl.endsWith("/")) {
                strUrl = strUrl + "/";
            }
            strUrl += "api/user/login/uid";
            Gson gson = new Gson();
            Map<String, String> mapHead = new HashMap<>();
            JSONObject obj = new JSONObject();
            //{
            //    "email": "77722675@qq.com",
            obj.put("email", passworkResultMainModel.getData().getEmail());
            //    "headImg": null,
            obj.put("headImg", passworkResultMainModel.getData().getHeadImg());
            //    "name": null,
            obj.put("name", passworkResultMainModel.getData().getName());
            //    "ticket": "6fb64ff4-41af-4e56-855f-18d285fb068b",
            obj.put("ticket", passworkResultMainModel.getData().getTicket());
            //    "uid": 2052632,
            obj.put("uid", passworkResultMainModel.getData().getUid());
            //    "wsId": 2,
            obj.put("wsId", passworkResultMainModel.getData().getWsId());
            //    "wsName": "专有轻流工作区",
            obj.put("wsName", passworkResultMainModel.getData().getWsName());
            //    "loginType": "EMAIL"
            obj.put("loginType", "EMAIL");
            //}


            String strBody=obj.toString();

            String strBack = HttpClientUtil.doPost(strUrl, strBody, mapHead);
            QingxiUserTokenResultMainModel tokenResultMainModel = gson.fromJson(strBack, QingxiUserTokenResultMainModel.class);
            if (tokenResultMainModel == null) {
                throw new Exception("获取用户Token失败,返回内容为空！");
            } else {
                if(tokenResultMainModel.getCode() == 0){
                    if (tokenResultMainModel.getData()==null||tokenResultMainModel.getData().getToken()==null||tokenResultMainModel.getData().getToken().isEmpty()){
                        throw new Exception("获取用户Token失败,返回内容为空！");
                    }
                    else{
                        _QingxiUserToken = tokenResultMainModel.getData().getToken();
                    }
                }
            }

        }

        return _QingxiUserToken;

    }
}
